//
//  LMQQCell.m
//  15qq
//
//  Created by apple on 15/8/4.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "LMQQCell.h"
#import "LMQQFrame.h"
#import "LMQQ.h"
@interface LMQQCell ()

@property (nonatomic,strong) UILabel *timeView;
@property (nonatomic,strong) UIImageView *iconView;
@property (nonatomic,strong) UIButton *textView;


@end

@implementation LMQQCell

+(instancetype)qqcellwithTableView:(UITableView *)tableView
{
    static NSString *cook = @"qq";
    LMQQCell *cell = [tableView dequeueReusableCellWithIdentifier:cook];
    if (cell==nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cook];
    }
    return cell;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self ==[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        //时间
        UILabel *timeView = [[UILabel alloc] init];
        [self.contentView addSubview:timeView];
        timeView.textAlignment = NSTextAlignmentCenter;
        timeView.font = [UIFont systemFontOfSize:15];
        self.timeView = timeView;
        
        //头像
        UIImageView *iconView = [[UIImageView alloc] init];
        
        [self.contentView addSubview:iconView];
        self.iconView = iconView;
        
        //文字
        UIButton *textView = [[UIButton alloc] init];
        textView.titleLabel.numberOfLines = 0;
        textView.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
        [self.contentView addSubview:textView];
        textView.titleLabel.font = [UIFont systemFontOfSize:13];
        
        
        self.textView = textView;
        
//        self.textView.backgroundColor = [UIColor redColor];
        
        self.textView.contentEdgeInsets = UIEdgeInsetsMake(15, 15, 15, 15);
    }
    
    return self;
}

-(void)setQqframe:(LMQQFrame *)qqframe
{
    _qqframe = qqframe;
    
    
    self.timeView.text = _qqframe.qq.time;
    //设置内容
    self.textView.titleLabel.text=_qqframe.qq.text;
    
    
    [self.textView setTitle:_qqframe.qq.text forState:UIControlStateNormal];
    if (0 == _qqframe.qq.type) {
        self.iconView.image = [UIImage imageNamed:@"me"];
        
    
        [self.textView setBackgroundImage:[self backgroudImage:@"chat_send_nor"] forState:UIControlStateNormal];
        [self.textView setBackgroundImage:[self backgroudImage:@"chat_send_press_pic"] forState:UIControlStateHighlighted];

    }else
    {
        self.iconView.image = [UIImage imageNamed:@"other"];
        [self.textView setBackgroundImage:[self backgroudImage:@"chat_recive_nor"] forState:UIControlStateNormal];
        [self.textView setBackgroundImage:[self backgroudImage:@"chat_recive_press_pic"] forState:UIControlStateHighlighted];
    }
    
    [self.textView setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
    
    //设置位置
    
    self.timeView.frame = qqframe.timeFrame;
    self.iconView.layer.cornerRadius = 15;
    self.iconView.layer.masksToBounds = YES;
    self.iconView.frame = qqframe.iconFrame;
    self.textView.frame = qqframe.textFrame;
    
    
    
}

- (UIImage *) backgroudImage:(NSString *) imageStr
{
    UIImage *image = [UIImage imageNamed:imageStr];
//    CGFloat w = image.size.width * 0.5;
//    CGFloat h = image.size.height * 0.5;
//    
//    UIImage *newImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(h, w, h, w) resizingMode:UIImageResizingModeStretch];
    
    image  = [image stretchableImageWithLeftCapWidth:image.size.width*0.5 topCapHeight:image.size.height*0.5];
    
    return  image;
}

@end
